#include "operation.h"
#include "db.h"
#include "global.h"
#include "u.h"
#include "user.h"
#include <qdatetime.h>
#include <qdebug.h>

Operation::Operation(QObject *parent)
    : QAbstractListModel(parent)
{
    qDebug() << __FUNCTION__ << __LINE__;
    // 可以立即加载初始数据
    DB* db = DB::getInstance();
    m_roles = db->getTableFieldRole("log");
    loadOperation("");
}

// bool Operation::addOperation(QString product, QString workShop)
// {
//     qDebug() << __FUNCTION__ << __LINE__;
//     User* pUser = (User*)Global::instance().get("userInfo");
//     QString loginName = pUser->currentLogin()["name"].toString();
//     QString realName = pUser->currentLogin()["real_name"].toString();
//     // DB::getInstance()->addUser(name, password, real_name, expiration, status, permission, create_by);
//     DB::getInstance()->addOperation(loginName, realName, m_OperationNo, product, workShop, U::getDateTimeSTD(), loginName);
//     m_product = product;
//     m_workShop = workShop;
//     loadOperation("");
//     return true;
// }

// bool Operation::updateOperation(QString desc)
// {
//     qDebug() << __FUNCTION__ << __LINE__;
//     DB::getInstance()->updateOperation(desc, U::getDateTimeSTD(), m_OperationNo, m_product, m_workShop);
//     loadOperation("");
//     return true;

// }

// QVariantMap Operation::get(int row) const
// {
//     qDebug() << __FUNCTION__ << __LINE__;
//     QVariantMap result;
//     if (row < 0 || row >= m_records.size())
//         return result;
//     const QHash<QString,QVariant> &record = m_records[row];
//     for (auto it = record.begin(); it != record.end(); ++it) {
//         result[it.key()] = it.value();
//     }
//     return result;
// }

int Operation::rowCount(const QModelIndex &parent) const
{
    // qDebug() << __FUNCTION__ << __LINE__;
    return m_records.size();
}

QVariant Operation::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_records.size())
        return QVariant();

    //qDebug() << __FUNCTION__ << __LINE__ << "role:" << role << "-index:" << index.row();
    const QHash<QString, QVariant> has = m_records[index.row()];

    return has[m_roles[role]];
}

QHash<int, QByteArray> Operation::roleNames() const
{
    qDebug() << __FUNCTION__ << __LINE__ ;
    return m_roles;
}

bool Operation::insertOperationLog(const QString &type, const QString &desc, const QString keyInfo)
{
    User* pUser = (User*)Global::instance().get("userInfo");
    QString loginName = pUser->currentLogin()["name"].toString();
    QString realName = pUser->currentLogin()["real_name"].toString();

    DB::getInstance()->addLog(loginName, realName, type, desc, keyInfo, loginName);
    return true;
}


// QString Operation::OperationNo()
// {
//     qDebug() << "OperationNo" << m_OperationNo;
//     return m_OperationNo;
// }

// void Operation::setOperationNo(QString OperationNo)
// {
//     qDebug() << "setOperationNo" << OperationNo;
//     this->m_OperationNo = OperationNo;
// }


bool Operation::loadOperation(const QString& filter)
{
    qDebug() << __FUNCTION__ << __LINE__;
    DB* db = DB::getInstance();
    beginResetModel();
    m_records = db->queryOperation(filter);
    endResetModel();
    return true;
}
